/**
 * 项目版本添加接口测试用例
 *
 * Created by nongzhihzhong on 2021/11/22.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const common = require('../../../common');
const Administrator = require('../../../../models/administrator');
const uri = require('../../uri');
const { coverageKind } = require('../../../../../phoenix/models/tests');
const { regressionStatusPrompt, statusKind } = require('../../../../../phoenix/models/project-versions');
// const rules = require('./../../../../benchmark/static-analyze/gjb.json');

module.exports = describe('/PUT project-versions/:id.json', function () {
  const administrator = new Administrator();
  let project = {};
  let projectVersion = {};
  const newUserIds = [];
  // const analyzeTemplate = '';
  let newUser;
  function url(versionId) {
    if (!versionId) {
      versionId = projectVersion.versionId;
    }
    return `/project-versions/${versionId}.json`;
  }
  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        newUser = tester;
        return newUser.createRandomProject();
      })
      // .then((tester) => {
      //   newUserIds.push(tester.userId);
      //   newUser = tester;
      //   return tester.createTemplate(rules.verified_rules);
      // })
      // .then((template) => {
      //   analyzeTemplate = template._id.toString();
      //   return newUser.createRandomProject();
      // })
      .then((res) => {
        project = res;
        return project.createRandomVersion();
      })
      .then((res) => {
        projectVersion = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'put');
  common.should.be.tester(uri, url, 'put');

  // 编辑集成测试版本信息
  [
    {
      unitTest: {
        eventReminder: {
          configConfirmed: Math.random() > 0.5,
          regressionStatusPrompt: [
            regressionStatusPrompt.none,
            regressionStatusPrompt.removeOriginalVersion,
            regressionStatusPrompt.modifyOriginalVersion,
          ][Math.floor(Math.random() * 3)],
          firstVisit: Math.random() > 0.5 ? 1 : 0,
          parsed: Math.random() > 0.5,
          defectsCleared: [0, 1, 2][Math.floor(Math.random() * 3)],
        },
        timeoutTestGen: 10,
        timeoutTestRun: 10,
        strictOutputCheck: Math.random() > 0.5,
        coverages: [
          coverageKind.statement,
          coverageKind.branch,
          coverageKind.mcdc,
        ][Math.floor(Math.random() * 3)],
        status: [statusKind.initial, statusKind.normal][Math.floor(Math.random() * 2)],
      },
    },
    {
      staticAnalyze: {
        eventReminder: {
          configConfirmed: Math.random() > 0.5,
          regressionStatusPrompt: [
            regressionStatusPrompt.none,
            regressionStatusPrompt.removeOriginalVersion,
            regressionStatusPrompt.modifyOriginalVersion,
          ][Math.floor(Math.random() * 3)],
          firstVisit: Math.random() > 0.5 ? 1 : 0,
          parsed: Math.random() > 0.5,
          defectsCleared: [0, 1, 2][Math.floor(Math.random() * 3)],
        },
        // analyzeTemplate: '',
        status: [statusKind.initial, statusKind.normal][Math.floor(Math.random() * 2)],
      },
    },
    {
      integrationTest: {
        eventReminder: {
          configConfirmed: Math.random() > 0.5,
          defectsCleared: [0, 1, 2][Math.floor(Math.random() * 3)],
          regressionStatusPrompt: [
            regressionStatusPrompt.none,
            regressionStatusPrompt.removeOriginalVersion,
            regressionStatusPrompt.modifyOriginalVersion,
          ][Math.floor(Math.random() * 3)],
          firstVisit: Math.random() > 0.5 ? 1 : 0,
          parsed: Math.random() > 0.5,
        },
        timeoutTestRun: 10,
        strictOutputCheck: Math.random() > 0.5,
        coverages: [
          coverageKind.statement,
          coverageKind.branch,
          coverageKind.mcdc,
          coverageKind.invoke,
          coverageKind.invokedFunc,
        ][Math.floor(Math.random() * 5)],
        status: [statusKind.initial, statusKind.normal][Math.floor(Math.random() * 2)],
        fileRelation: Math.random() > 0.5,
      },
    },
  ].forEach((edit) => {
    it(`it should PUT a project ${Object.keys(edit)[0]} version with ${JSON.stringify(edit)}`, function (done) {
      const key = Object.keys(edit)[0];
      project.creator.login()
        // .then(() => {
        //   if (key === 'staticAnalyze') {
        //     edit.staticAnalyze.analyzeTemplate = analyzeTemplate;
        //   }
        // })
        .then(() => {
          chai.request(uri)
            .put(url())
            .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
            .send(edit)
            .end((err, res) => {
              res.should.have.status(200);
              common.should.be.includesAttribute(res.body[key], edit[key]);
              done();
            });
        })
        .catch(done);
    });
  });
});
